﻿using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LuaInterface;

namespace Winz
{

    class LuaChecker:IFileChecker
    {
        private string wowPath;
        string backupPath = @".\backup\";
        private string account;
        bool _isDone = false;
        public string accountPath { get { return Path.Combine(wowPath, @"WTF\Account", account); } }
        IEnumerable<string> fileNames;
        Lua lua;


        public LuaChecker(string wowPath, string account) {
            this.wowPath = wowPath;
            this.account = account;
            fileNames = new string[] { "TradeSkillMaster_Crafting", 
                                       "TradeSkillMaster_Auctioning",
                                       "TradeSkillMaster_Mailing",
                                       "ArkInventory"
                                      };
            lua = new Lua();
        }

        public bool Check()
        {
            foreach (string f in fileNames)
            {
                if (!Check(f))
                    return false;
            }
            return true;
        }


        public bool Check(string name) {
            string filename = Path.Combine(wowPath, @"WTF\Account", account,"SavedVariables",name + ".lua");
            if (!File.Exists(filename))
            {
                Log.Debug("LuaChecker : {0} not found", filename);
                return false;
            }

            try
            {
                lua.DoFile(filename);
                LuaTable t = lua.GetTable(name + "DB.profiles.CheckProfile");
                if (t != null)
                    return true;
            }
            catch (Exception ex)
            {
                Log.Debug("[Erreur] Source :{0} Msg:{1} {2}", ex.Source, ex.Message, ex.StackTrace);
            }

            Log.Debug("LuaChecker Need Correction: {0}", filename);
            return false;
        }

        public void Correct() {
            string filename;
            string filenameFrom;
            foreach (string f in fileNames) 
            { 
                filename = Path.Combine(wowPath, @"WTF\Account", account,"SavedVariables",f + ".lua");
                try
                {
                    if (!Check(f))
                    {
                        filenameFrom = null;
                        if (File.Exists(Path.ChangeExtension(filename, "luasav")))
                            filenameFrom = Path.ChangeExtension(filename, "luasav");
                        else
                            filenameFrom = Path.Combine(backupPath, f + ".lua");

                        if (filenameFrom != null)
                        {
                            File.Copy(filenameFrom, filename, true);
                            Log.Debug("Copied {0} to {1}", filenameFrom, filename);
                        }
                    }
                    else
                    {
                        File.Copy(filename, Path.ChangeExtension(filename, "luasav"), true);
                    }
                }
                catch
                {
                }
            }
        }

    }
}
